package com.example.wm.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.entity.BaoxiaoImg;

import com.example.wm.mapper.ReimbursementImagesMapper;
import com.example.wm.service.ReimbursementImagesService;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.sql.Blob;
import java.util.Base64;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.awt.image.BufferedImage;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.web.multipart.MultipartFile;


@Slf4j
@RestController
@RequestMapping("/ReimbursementImages")
public class ReimbursementImagesController {
    @Resource
    private ReimbursementImagesService reimbursementImagesService;
    @Resource
    private ReimbursementImagesMapper reimbursementImagesMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/getList")
    public Result<List> getReimbursementList(@RequestBody JSONObject jsonObject) throws IOException {
        Integer id=jsonObject.getInteger("id");
//        if(redisTemplate.hasKey("getListimg"+id)){
//         Object idlist=   redisTemplate.opsForValue().get("getListimg"+id);
//            return  ResultData.success("操作成功",idlist);
//
//        }else {
//        Set keys=redisTemplate.keys("getListimg*");
//        redisTemplate.delete(keys);
            LambdaQueryWrapper<BaoxiaoImg> lq=new LambdaQueryWrapper<>();
            lq.eq(BaoxiaoImg::getMxid,id);
            List<BaoxiaoImg> l1=reimbursementImagesService.list(lq);
            ArrayList<String> al=new ArrayList<>();
            for(BaoxiaoImg i:l1){
                String base64EncodedString = Base64.getEncoder().encodeToString(i.getImg());
                String re="data:image/jpeg;base64,"+base64EncodedString;
                al.add(re);


            }
//            redisTemplate.opsForValue().set("getListimg"+id,al);
            return  ResultData.success("操作成功",al);
//        }


    }

    @PostMapping("/transterurl")
    public Result<String> transterurlReimbursementList(@RequestBody MultipartFile multipartFile) throws IOException {

        byte[] ss=null;
        String base64EncodedString=null;
        try{
            ss = multipartFile.getBytes();
             base64EncodedString = Base64.getEncoder().encodeToString(ss);

        }catch(IOException e){
            e.printStackTrace();
        }
        String re=base64EncodedString;
        return  ResultData.success("操作成功",re);
    }

    @PostMapping("/putimg")
    public Result<String> putReimbursementList(@RequestBody BaoxiaoImg tp)  {

        reimbursementImagesService.save(tp);

        return  ResultData.success("操作成功");
    }
    //删除
    @PostMapping("/delete")
    public Result<String> deleteReimbursementList(@RequestBody JSONObject jsonObject)  {
        reimbursementImagesService.removeById(jsonObject.getString("mxid"));
        return  ResultData.success("操作成功");
    }
    public static String blobToBase64String(Blob blob) {
        String resBase64 = "";
        if(null != blob) {
            try {
                InputStream inputStream = blob.getBinaryStream();
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int n = 0;
                while (-1 != (n = inputStream.read(buffer))) {
                    outputStream.write(buffer, 0, n);
                }
//                resBase64 =new BASE64Encoder().encode(outputStream.toByteArray()) ;
                outputStream.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(resBase64);
            return "data:image/jpeg;base64,"+resBase64;
        }else {
            return null;
        }
    }
}
